﻿using System;
using System.Reflection;

namespace Script_Text_Editor_WPF_Edition.Library
{
    class NETWrapper : ILibrary
    {
        private object target;
        private Type type;

        /// <summary>
        /// 获取当前类型的内部编号
        /// </summary>
        public static int Index
        {
            get { return 2; }
        }

        #region Interface methods

        public string GetLibraryInfo()
        {
            return (string)Invoke("DLLGetLibraryInfo", null);
        }

        public string GetSupportExtensions()
        {
            return (string)Invoke("DLLGetSupportExtensions", null);
        }

        public string GetStringBreakLineChar()
        {
            return (string)Invoke("DLLGetStringBreakLineChar", null);
        }

        public bool GetCanMakeNewLine()
        {
            return (bool)Invoke("DLLGetCanMakeNewLine", null);
        }

        public string GetNewLineBreakChar()
        {
            return (string)Invoke("DLLGetNewLineBreakChar", null);
        }

        public string GetStringCollection(byte[] file)
        {
            return (string)Invoke("DLLGetStringCollection", new object[] { file });
        }

        public byte[] SetStringCollection(string file)
        {
            return (byte[])Invoke("DLLSetStringCollection", new object[] { file });
        }

        #endregion Interface methods

        #region Wrapper

        public NETWrapper(string library)
        {
            type = Assembly.LoadFrom(library).GetType("Text_Library.Program");
            target = type.InvokeMember("Program", BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, null, null);
        }

        public NETWrapper(Assembly assembly)
        {
            type = assembly.GetType("Text_Library.Program");
            target = type.InvokeMember("Program", BindingFlags.CreateInstance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, null, null);
        }

        private object Invoke(string strMethod, object[] args)
        {
            return (object)type.InvokeMember(strMethod, BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly, null, target, args);
        }

        #endregion Wrapper
    }
}